Set the working directory and read in the company names and ISINs
setwd("H:/COVID - Disruption of Value Chains")
library(readxl)
Manu <- read_excel("H:/COVID - Disruption of Value Chains/Consumer Electronic Companies.xlsx",col_names = FALSE)
Semi <- read_excel("H:/COVID - Disruption of Value Chains/Consumer Electronic Companies.xlsx",sheet = "Semiconductor Supply",col_names = FALSE)
EMS <- read_excel("H:/COVID - Disruption of Value Chains/Consumer Electronic Companies.xlsx",sheet = "EMS", col_names = FALSE)
Comp <- read_excel("H:/COVID - Disruption of Value Chains/Consumer Electronic Companies.xlsx",sheet = "Other Components & Distribution",col_names = FALSE)
Sili <- read_excel("H:/COVID - Disruption of Value Chains/Consumer Electronic Companies.xlsx",sheet = "Silicon & Others",col_names = FALSE)
Connect to the Thompsom Reuters Eikon API using the
get_symbology function to convert ISINs to into RICs, which
can then be used to download the stock price data. (Regarding the
download, I used Thompson Reuters EIKON’s Excel Add-in.)
library(eikonapir)
eikonapir::set_proxy_port(9000L)
eikonapir::set_app_id('921948f635704208bea4b68f8a1add2129749be0')
ric_Manu = get_symbology(as.list(Manu[[2]]), from_symbol_type="ISIN", to_symbol_type="RIC")
ric_Semi = get_symbology(as.list(Semi[[2]]), from_symbol_type="ISIN", to_symbol_type="RIC")
ric_EMS = get_symbology(as.list(EMS[[2]]), from_symbol_type="ISIN", to_symbol_type="RIC")
ric_Comp = get_symbology(as.list(Comp[[2]]), from_symbol_type="ISIN", to_symbol_type="RIC")
ric_Sili = get_symbology(as.list(Sili[[2]]), from_symbol_type="ISIN", to_symbol_type="RIC")
# Merge into one column
RICS=rbind(ric_EMS[2],ric_Semi[2],ric_Sili[2])
library(openxlsx)
# write.xlsx(RICS,"RICS_Electronics.xlsx")
Read the raw material returns of ferrosilicon (futures) and format and select the relevant columns
Sili_data=read.csv("H:/COVID - Disruption of Value Chains/Ferrosilicon Futures.csv")
library(lubridate)
Sili_data$Date <- parse_date_time(Sili_data$Date,"%m/%d/%Y")
Sili_data=Sili_data[order(as.Date(Sili_data$Date, format="%d/%m/%Y")),]
Sili_data=Sili_data[,c(1,3,2,4,5)]
colnames(Sili_data)=c("Date","Price Open Sili","Price Close Sili","Price High Sili","Price Low Sili")
Further process data into desired format:
library(tidyr)
Data = read_excel("H:/COVID - Disruption of Value Chains/Daily Stock Prices - Consumer Electronics.xlsx",sheet = "Data")
Date = Data[,1] #525:1347
colnames(Data)[1]="Date"
Data_2 = left_join(Data,Sili_data, by = "Date")
Data_2=Data_2[231:1347,-c(1:41)]
Data_2=Data_2 %>% fill(c(colnames(Data_2)), .direction = "downup")
Data=Data[,-c(1:41)]
Data = as.data.frame(lapply(lapply(Data, as.numeric),log))
Data_2 = as.data.frame(lapply(lapply(Data_2, as.numeric),log))
Calculate the volatity of the stock prices according to the formula for range-base-realized volatility by Garman and Klass (1980)
Vol_data=0
for (i in 1:(ncol(Data)/4)){
tmp <- 0.511*((Data[,(i*4)-1]-Data[,(i*4)])^2)-0.019*((Data[,(i*4)-2]-Data[,(i*4)-3])*(Data[,(i*4)-1]+Data[,(i*4)]-(2*Data[,(i*4)-3]))-(2*(Data[,(i*4)-1]-Data[,(i*4)-3])*(Data[,(i*4)]-Data[,(i*4)-3])))-0.383*((Data[,(i*4)-2]-Data[,(i*4)-3])^2)
Vol_data = cbind(Vol_data,tmp)
print(i)
}
Vol_data=Vol_data[,-1]
Vol_data_2=0
for (i in 1:(ncol(Data_2)/4)){
tmp <- 0.511*((Data_2[,(i*4)-1]-Data_2[,(i*4)])^2)-0.019*((Data_2[,(i*4)-2]-Data_2[,(i*4)-3])*(Data_2[,(i*4)-1]+Data_2[,(i*4)]-(2*Data_2[,(i*4)-3]))-(2*(Data_2[,(i*4)-1]-Data_2[,(i*4)-3])*(Data_2[,(i*4)]-Data_2[,(i*4)-3])))-0.383*((Data_2[,(i*4)-2]-Data_2[,(i*4)-3])^2)
Vol_data_2 = cbind(Vol_data_2,tmp)
print(i)
}
Vol_data_2=Vol_data_2[,-1]
Define shortcut names for the companies (on vector including the raw material price and one omitting it)
names = c("SYNO","CADE","CEVA","VERI","RAMB",
"APMA","ASML","LAMR","TOEL","KLA","SCRE",
"TSCM","SAMS","UMC","SMIC", "TOWE",
"BRCO","QUCO","NVDI","MEDT","AMD", "XILI",
"INTL","SKHY","MICR","TI","STM","NXP","ANDE","INF","ON",
"ASE","AMKO","JCET","TIHU","POWE","TONF","KING",
"SHIN","TOKU","SUMC","GLOB","NSI","WACK","SILT","MITS","REC","OSAK","GCL","TONW","OCI","DAQO","HEML")
names2 = c("FOXC","PEGA","JABI","FLEX","WIST","SANM","KINP","BYDE","USI",
"SYNO","CADE","CEVA","VERI","RAMB",
"APMA","ASML","LAMR","TOEL","KLA","SCRE",
"TSCM","SAMS","UMC","SMIC", "TOWE",
"BRCO","QUCO","NVDI","MEDT","AMD", "XILI",
"INTL","SKHY","MICR","TI","STM","NXP","ANDE","INF","ON",
"ASE","AMKO","JCET","TIHU","POWE","TONF","KING",
"SHIN","TOKU","SUMC","GLOB","NSI","WACK","SILT","MITS","REC","OSAK","GCL","TONW","OCI","DAQO","HEML",
"SILI")
Define the function for the reduced-form impulse response function (IRF)
colnames(Vol_data)<- names
colnames(Vol_data_2)<- names2
#Phi = Reduced-Form IRFs or Coefficient Matrices of the MA-representation
compute_Phi = function(p, k, A_comp, n.ahead) {
J = matrix(0,nrow=k,ncol=k*p)
diag(J) = 1
Phi = lapply(1:n.ahead, function(i) {
J %*% (A_comp %^% (i-1)) %*% t(J)
})
return(Phi)
}
Define the function for the generalized forward error variance decomposition (FEVD)
library(frequencyConnectedness)
genFEVD_cus = function(
A,
Sigma,
n.ahead,
no.corr=F
) {
k = dim(A)[1] # number of equations
p = dim(A)[2]/k # number of lags
# Turn into companion form:
if (p>1) {
A_comp = BigVAR::VarptoVar1MC(A,p,k)
} else {
A_comp = A
}
# Set off-diagonals to zero:
if (no.corr) {
Sigma = diag(diag(Sigma))
}
Phi = compute_Phi(p,k,A_comp,n.ahead+1) # Reduced-form irfs
denom = diag(Reduce("+", lapply(Phi, function(i) i %*% Sigma %*%
t(i))))
enum = Reduce("+", lapply(Phi, function(i) (i %*% Sigma)^2))
tab = sapply(1:nrow(enum), function(j) enum[j, ]/(denom[j] *
diag(Sigma)))
tab = t(apply(tab, 2, function(i) i/sum(i)))
return(tab)
}
Now the connectedness matrices (volatility spillover from each firm i to each firm j) for each rolling window can be calculated.
# BigVAR ----
library(BigVAR)
library(expm)
library(data.table)
library(vars)
library(dplyr)
# Create model and calculate Conectedness Matrix for each quarter
w=150 #window size
Con_Matrices_2 <- vector("list",((nrow(Vol_data_2)-w)/5))
for (i in 1:((nrow(Vol_data_2)-w)/5)){
p = 1 # lags
k = ncol(as.data.frame(Vol_data_2[((i*5)+1):(w+(i*5)),]) %>% select_if(~ !any(is.na(.)))) # number of equations dynamically adjusted for missing data
N = nrow(Vol_data_2[((i*5)+1):(w+(i*5)),]) - p # number of obs - lags
# k = ncol(as.data.frame(Vol_data[((i*5)+1):(w+(i*5)),]) %>% select_if(~ !any(is.na(.)))) # number of equations dynamically adjusted for missing data
# N = nrow(Vol_data[((i*5)+1):(w+(i*5)),]) - p # number of obs - lags
# Create an Elastic Net with maximum lag order p=4, 10 gridpoints with lambda optimized according to rolling validation of 1-step ahead MSFE
mod1 = constructModel(as.matrix(as.data.frame(Vol_data_2[((i*5)+1):(w+(i*5)),]) %>% select_if(~ !any(is.na(.)))),p,struct = "BasicEN",
gran=c(450,10),intercept = FALSE,alpha = 0.5,RVAR=FALSE,h=10,cv="Rolling",
MN=FALSE,verbose=FALSE,IC=TRUE)#check alpha and dual values
results = cv.BigVAR(mod1)
# Get model estimates
A = results@betaPred[,2:ncol(results@betaPred)] # (k x (k*p)) = coefficient matrix (reduced form)
Sigma = crossprod(results@resids)/(N-(k*p)-1) # calc. the covariance matrix of errors
#Normalize because Columns !=1 in generalized VAR setup
Con_Matrices_2[[i]] = as.matrix(genFEVD_cus(A,Sigma,10)) %*% diag(1/colSums(genFEVD_cus(A,Sigma,10)))
colnames(Con_Matrices_2[[i]]) = colnames(as.data.frame(Vol_data_2[((i*5)+1):(w+(i*5)),]) %>% select_if(~ !any(is.na(.))))
diag(Con_Matrices_2[[i]])<-0
print(i)
}
save.image("Conntectedness VAR - Consumer Electronics-ohne EMS.R.RData")
First the created matrices which were saved in the workspace.
# loading the workspace
load("C:\\Users\\Dirk\\OneDrive - Universität Münster\\6. Promotion\\COVID - Disruption of Value Chains\\Conntectedness VAR - Consumer Electronics-ohne EMS.R.RData")
The loaded excel file assigns coutnries to each company. Now we can group the companies by their region (Asia, Europe, US, and Rest of the World)
country = read_excel("C:\\Users\\Dirk\\OneDrive - Universität Münster\\6. Promotion\\COVID - Disruption of Value Chains\\RICS_Electronics.xlsx",sheet="Countries")
country = country[-10,-1] #drop an irrelevant company, probably better to use the name next time xD
country = country[-c(35,36,38,39,40,41,42,55,56),]
for (i in 1:nrow(country)){
if(country[i,1]== "CH" | country[i,1]=="DE" | country[i,1]=="NO" | country[i,1]=="FR"
| country[i,1]== "GB"| country[i,1]=="NL"){
country[i,2]= "Europe"
} else if (country[i,1]== "CA" | country[i,1]=="US"){
country[i,2]= "North America"
} else if (country[i,1]== "CN" | country[i,1]=="HK" | country[i,1]== "JP" | country[i,1]=="KR"| country[i,1]=="TW"){
country[i,2]= "Asia"
} else {country[i,2]= "Rest" }
}
Define different shapes for each region to distinguish them in the plot.
for (i in 1:nrow(country)){
if(country[i,1]== "CH" | country[i,1]=="DE" | country[i,1]=="NO" | country[i,1]=="FR"
| country[i,1]== "GB"| country[i,1]=="NL"){
country[i,3]= "circle"
} else if (country[i,1]== "CA" | country[i,1]=="US"){
country[i,3]= "square"
} else if (country[i,1]== "CN" | country[i,1]=="HK" | country[i,1]== "JP" | country[i,1]=="KR"| country[i,1]=="TW"){
country[i,3]= "triangle"
} else {country[i,3]= "diamond" }
}
Add the supply chain level of each company to the plot_info dataframe, to distinguish between the different levels of the supply chain in the plot.
plot_info = cbind(names2,c(rep("IP",5),rep("SME",6),rep("Foundry",5),rep("Fabless",6),rep("IDM",9),rep("OSAT",7),rep("Sili",15),"Raw Material Price"),
c(rep("blue4",5),rep("blue3",6),rep("deepskyblue3",5),rep("skyblue1",6),rep("lightblue1",9),rep("gray85",7),rep("limegreen",15),NA),
rbind(country[,c(2,3)],c(NA,NA)))
for (i in 1:nrow(plot_info)){
if(plot_info[i,2]== "OSAT"| plot_info[i,2]=="Foundry" )
{plot_info[i,6]= 4}
else if (plot_info[i,2]== "IP"| plot_info[i,2]=="Fabless" )
{plot_info[i,6]= 3}
else if (plot_info[i,2]== "SME"| plot_info[i,2]=="IDM" )
{plot_info[i,6]= 1}
else if (plot_info[i,2]== "Sili")
{plot_info[i,6]= 8}
else {plot_info[i,6]= 6}
}
colnames(plot_info)<-c("Name","Level","Color","Country","Shape","Polygon")
plot_info= rbind(plot_info,data.frame(Name=c("Rare Earth Metals","Copper","Precious Metals","Silicon"),Level=rep("Raw Material Price",4),Color=rep("orange",4),Country=rep("None",4),Shape=rep("circle",4),Polygon=rep(6,4)))
As we only want to look at connections between firms with a direct costumer-supplier relationship, we set the other connections to zero. Otherwise we would also measure the volatility spillovers between opposing business models like the situation when a fabless producer takes market share from an IDM.
for ( i in 1:((nrow(Vol_data_2)-w)/5)){
Con_Matrices_2[[i]][1:(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")),1 : (sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))] = 0
Con_Matrices_2[[i]][(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")+1) : (sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME")),(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")+1) : (sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))] = 0
Con_Matrices_2[[i]][(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+1) : (sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))),(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+1) : (sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry")))] = 0
Con_Matrices_2[[i]][(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless")),((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))] = 0
Con_Matrices_2[[i]][(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM")),((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))] = 0
Con_Matrices_2[[i]][(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT")),((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT"))] = 0
Con_Matrices_2[[i]][(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT"))+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Sili")),((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT"))+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Sili"))] = 0
}
for ( i in 1:((nrow(Vol_data_2)-w)/5)){
#IP
Con_Matrices_2[[i]][1 : ((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))),((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME")))]=0
Con_Matrices_2[[i]][1 : ((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))),((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+1) : ((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT"))+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Sili"))]=0
#SME
Con_Matrices_2[[i]][(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))),1:((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")))]=0
Con_Matrices_2[[i]][(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))),((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))]=0
#Foundry
Con_Matrices_2[[i]][((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))),((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless")+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))]=0
#Fabless
Con_Matrices_2[[i]][((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))),((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))]=0
Con_Matrices_2[[i]][((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))),((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless")+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))]=0
#IDM
Con_Matrices_2[[i]][((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))),((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))]=0
#OSAT
Con_Matrices_2[[i]][((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT"))),(1):(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))]=0
Con_Matrices_2[[i]][((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT"))),((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT"))+sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Sili"))]=0
#Sili
Con_Matrices_2[[i]][((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Sili"))),1:((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP")))]=0
Con_Matrices_2[[i]][((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Sili"))),((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+1):((sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IP"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="SME"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Foundry"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="Fabless"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="IDM"))+(sum(plot_info[match(colnames(Con_Matrices_2[[i]]),names2),2]=="OSAT")))]=0
}
For a first glance at the matrices and a verification if everything went well, I plotted some test matrices within R. Later for better visualization I used Gephi which enables more opportunities regarding the visualization of networks.
First one test network:
library(ForceAtlas2)
library(qgraph)
library(igraph)
i = 174
#change i before executing this
index = match(colnames(Con_Matrices_2[[i]]),names2)
plot_data = plot_info[index,]
edge_list = as_data_frame(graph_from_adjacency_matrix(Con_Matrices_2[[i]],weighted=TRUE))
coord=layout.forceatlas2(edge_list, iterations=8000, plotstep=0)
qgraph(Con_Matrices[[i]], layout = as.matrix(coord[,2:3]),
label.color = "black", label.font = 2, label.cex = 1, shape = plot_data[,5], labels = plot_data[,1], esize = 5,
maximum = max(Con_Matrices[[i]]), color = "white", node.width = 1.8,
edge.color = plot_data[,3], curve = 1, border.width = 4, border.color = plot_data[,3],
asize = 1.5, trans=0.75)
text(x = -0.9, y = 1, labels = substitute(paste(d), list(d = "2020")),
xpd = NA, cex = 1.5)
Then shutting off the plot console, a range of networks. Those are evaluated as tiffs in a file explorer.
for(i in 125:190){
index = match(colnames(Con_Matrices[[i]]),names)
plot_data = plot_info[index,]
edge_list = as_data_frame(graph_from_adjacency_matrix(Con_Matrices[[i]],weighted=TRUE))
coord=layout.forceatlas2(edge_list, iterations=8000, plotstep=0)
tiff(paste("plot_", i, ".tiff", sep = ""), units="px", width=991, height=567, res=800)
qgraph(Con_Matrices[[i]], layout = as.matrix(coord[,2:3]),
label.color = "black", label.font = 2, label.cex = 1, shape = plot_data[,5], labels = plot_data[,1], esize = 5,
maximum = max(Con_Matrices[[i]]), color = "white", node.width = 1.8,
edge.color = "white", curve = 1, border.width = 4, border.color = plot_data[,3],
asize = 1.5, trans=0.75,
mar=c(2,1,2,1))
dev.off()
}
Export the relevant graphs to Gephi, by creating an edgelist
colnames(plot_data)<-c("Name","Level","Color","Country","Shape","Polygon")
plot_data$ID <- cumsum(!duplicated(plot_data[,1]))
write.csv(plot_data[,c(1,2,4,6,7)],file = "Plot_data_Elec.csv", row.names = FALSE)
edge_list=cbind(edge_list,rep("weighted",nrow(edge_list)))
edge_list$Source <- cumsum(!duplicated(edge_list[,1]))
colnames(edge_list)<-c("From","Name","Weight","Type","Source")
edge_list <- merge(edge_list,plot_data[,c(1,7)],by="Name")
edge_list = edge_list[,c(2,1,3,4,5,6)]
colnames(edge_list)<-c("From","To","Weight","Type","Source","Target")
edge_list = edge_list[order(edge_list[,5],decreasing = FALSE),]
write.csv(edge_list,file = "Edgelist_Elec.csv",row.names = FALSE)
After using Gephi the graphs look like this: